<?php
/**
 * Created by PhpStorm.
 * User: Duck
 * Create time: 2016/12/21 9:56
 */

namespace D\Controller;
use Common\Controller\BaseController;

class ExamsController extends BaseController{
    public function show(){
        $a=M('dict')->where(array('field'=>array('in',array('exam','project'))))->select();
        $this->assign('examType',$a);
        $this->display();
    }
    public  function getName(){
        $where=[
            'class'=>I('post.class'),
        ];
        $res=M('students')->field('student_id,sname')->where($where)->select();
        $this->ajaxReturn($res);
    }
    public function add(){
        $score=explode(',',I('post.score'));
        $id=explode(',',I('post.id'));
        foreach($id as $k=>$v){
            $where=[
                'student_id'=>$v,
                'name_dict_id'=>I('post.exam'),
                'term'=>I('post.term')
            ];
            if(M('exams')->where($where)->find()){
                $res='学生成绩已登入，请勿重复添加';
                $this->ajaxReturn($res);
                exit;
            }
            $data=[
                'student_id'=>$v,
                'name_dict_id'=>I('post.exam'),
                'term'=>I('post.term'),
                'score'=>$score[$k],
                'examtime'=>date('Y/M/d',time()),
            ];
            if(M('exams')->data($data)->add()){
                continue;
            }else{
                $res='添加失败,请查询原因后重新添加';
                $this->ajaxReturn($res);
                exit;
            }
        }
        $res='添加成功';
        $this->ajaxReturn($res);
    }
}